import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'

Vue.use(VueRouter)

const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    { path: '/home', component: Home },
]



const router = new VueRouter({
        mode: 'hash',
        base: process.env.BASE_URL,
        routes
    })
    // 导航守卫钩子
router.beforeEach((to, from, next) => {
    // to 要去那
    // from 从哪里来
    // next 放行
    // 如果用户访问首页直接放行
    if (to.path === '/login') return next();
    const tokenStr = window.sessionStorage.getItem("token");
    if (!tokenStr) return next('/login');
    next();
})

export default router